package com.zzyl.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.RoleDto;
import com.zzyl.dto.UserDto;
import com.zzyl.service.UserService;
import com.zzyl.vo.UserVo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@Api(tags = "用户管理")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 分页查询
     * @param userDto
     * @param pageNum
     * @param pageSize
     * @return
     */
    @PostMapping("/page/{pageNum}/{pageSize}")
    @ApiOperation(value = "用户分页",notes = "用户分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userDto",value = "用户DTO对象",required = true,dataType = "userDto"),
            @ApiImplicitParam(paramType = "path",name = "pageNum",value = "页码",example = "1",dataType = "Integer"),
            @ApiImplicitParam(paramType = "path",name = "pageSize",value = "每页条数",example = "10",dataType = "Integer")
    })
    @ApiOperationSupport(includeParameters = {"userDto.dataState","userDto.deptNo","userDto.email"
            ,"userDto.postNo","userDto.realName","userDto.roleId"})
    public ResponseResult<PageResponse<UserVo>> selectPage(@RequestBody UserDto userDto,
                                                           @PathVariable Integer pageNum,
                                                           @PathVariable Integer pageSize){
        PageResponse<UserVo> page = userService.selectPage(userDto,pageSize,pageNum);
        return ResponseResult.success(page);
    }

    /**
     * 新增用户
     * @param userDto
     * @return
     */
    @PutMapping
    @ApiOperation(value = "用户新增",notes = "用户新增")
    @ApiImplicitParam(name = "userDto",value = "用户DTO对象",required = true,dataType = "userDto")
    @ApiOperationSupport(includeParameters = {"userDto.realName","userDto.roleVoIds","userDto.deptNo"
            ,"userDto.postNo","userDto.dataState","userDto.email","userDto.email"})
    public ResponseResult insertUser(@RequestBody UserDto userDto){
        userService.insert(userDto);
        return ResponseResult.success();
    }

    /**
     * 更新用户
     * @param userDto
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "用户修改",notes = "用户修改")
    @ApiImplicitParam(name = "userDto",value = "用户DTO对象",required = true,dataType = "userDto")
    @ApiOperationSupport(includeParameters = {"userDto.roleName","userDto.roleVoIds","userDto.deptNo","userDto.postNo","userDto.dataState","userDto.email","userDto.mobile"})
    public ResponseResult updateUser(@RequestBody UserDto userDto){
        userService.update(userDto);
        return ResponseResult.success();
    }

    /**
     * 启用或者禁用
     * 接口文档有错误 应该是Long不是Integer
     * @param id
     * @param status
     * @return
     */
    @PutMapping("/is-enable/{id}/{status}")
    @ApiOperation(value = "启用或者禁用")
    public ResponseResult startOrStop(@ApiParam(value = "id") @PathVariable Long id,
                                      @ApiParam(value = "status")@PathVariable String status){

        userService.startOrStop(id,status);
        return ResponseResult.success();
    }

    /**
     * 删除
     * 单个删除但是前端还是传批量删除
     * @param userIds
     * @return
     */
    @DeleteMapping("/remove/{userIds}")
    @ApiOperation(value = "删除")
    public ResponseResult deleteUser(@ApiParam(value = "userIds") @PathVariable Long userIds){
        userService.deleteById(userIds);
        return ResponseResult.success();
    }

    /**
     * 重置密码 接口文档依然写的String类型 但是直接用Long可以接受
     * @param userId
     * @return
     */
    @PostMapping("/reset-passwords/{userId}")
    @ApiOperation(value = "重置密码")
    public ResponseResult resetPassword(@PathVariable Long userId){
        boolean flag =  userService.resetPassword(userId);
        return ResponseResult.success(flag);
    }
}
